package com.flying.carclub.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.flying.carclub.BaseActivity;
import com.flying.carclub.R;
import com.flying.carclub.api.ApiImpl;
import com.flying.carclub.bean.UserInfoBean;
import com.flying.carclub.http.BaseBean;
import com.flying.carclub.http.OnResponseListener;
import com.flying.carclub.utils.SPUtils;
import com.flying.carclub.utils.StringUtil;
import com.flying.carclub.widget.TitleBar;
import com.stonesun.newssdk.NewsAgent;
import com.stonesun.newssdk.bean.UserInfo;
import com.stonesun.newssdk.itf.AppUserItf;

public class LoginActivity extends BaseActivity {
    private EditText phoneEt;
    private EditText pwdEt;
    private Button loginoutBtn;
    private Button loginBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        phoneEt = findViewById(R.id.et_phone);
        pwdEt = findViewById(R.id.et_pwd);
        loginoutBtn = findViewById(R.id.bt_loginout);
        loginBtn = findViewById(R.id.bt_login);

        initListener();
    }

    private void initListener() {
//        loginoutBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                NewsAgent.logOutComment();
//            }
//        });

        TitleBar titleBar  = findViewById(R.id.title_bar);
        titleBar.setOnTitleClickListener(new TitleBar.OnTitleClickListener() {
            @Override
            public boolean onLeftClick() {
                return false;
            }

            @Override
            public void onRightClick() {
                startActivityForResult(new Intent(LoginActivity.this, SignupActivity.class), 100);
            }
        });

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(phoneEt.getText())) {
                    showToast("请输入手机");
                    return;
                }

                if (!StringUtil.isMobileNumber(phoneEt.getText().toString())) {
                    showToast("请输入11位正确的手机号码");
                    return;
                }
                if (TextUtils.isEmpty(pwdEt.getText()) || pwdEt.getText().length() < 6) {
                    showToast("密码错误");
                    return;
                }
                new ApiImpl().httpLogin(phoneEt.getText().toString(), pwdEt.getText().toString(), new OnResponseListener() {
                    @Override
                    public void onSuccess(Object response) {
                        showToast("登录成功");
                        if (response != null){
                            UserInfoBean userInfoBean = (UserInfoBean) response;
                            if (userInfoBean != null) {
                                SPUtils.setUserInfo(LoginActivity.this, userInfoBean);
                            }
                        }

                        AppUserItf appUserItf=new AppUserItf() {
                            @Override
                            public boolean isLogin() {
                                return true;
                            }

                            @Override
                            public UserInfo getUserinfo() {
                                UserInfo userInfo=new UserInfo();
                                userInfo.setUid("589");
                                userInfo.setToken("token");
                                userInfo.setUsericon("");
                                userInfo.setUsername("用户名");
                                userInfo.setFrom("QQ");
                                return userInfo;
                            }
                        };
                        NewsAgent.SetUserImpForSDK(appUserItf,LoginActivity.class);

                        setResult(RESULT_OK);
                        LoginActivity.this.finish();
                    }

                    @Override
                    public void onError(String err) {
                        if (TextUtils.isEmpty(err)){
                            err = "网络请求失败";
                        }
                        showToast(err);
                    }
                });


            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            setResult(RESULT_OK);
            LoginActivity.this.finish();
        }
    }
}

